var Tetris = function () {}

Tetris.gameApp = null;
Tetris.embed = function(container,width,height,brickSize) 
{
	if(!document.createElement('canvas').getContext)
	{
		alert("Your browser doesn't support canvas!");
		return;
	}

	var gameApp = new Tetris();
	gameApp.gameContainer = document.getElementById(container);
	gameApp.containerWidth = width;
	gameApp.containerHeight = height;
	gameApp.brickSize = brickSize;
	gameApp.initialize();
	
	Tetris.gameApp = gameApp;
}

Tetris.PREVIEW_BRICK_SIZE = 10;
Tetris.PREVIEW_AREA_WIDTH = 100;
Tetris.PREVIEW_CANVAS_WIDTH = 60;
Tetris.PREVIEW_CANVAS_HEIGHT = 60;
Tetris.PREVIEW_AREA_SPACE = 20;
Tetris.BUTTON_SPACE = 10;
Tetris.BUTTON_WIDTH = 80;
Tetris.BUTTON_SIZE = 22;
Tetris.PLAY_BUTTON_TEXT = "PLAY";
Tetris.RANKING_BUTTON_TEXT = "RANKING";

Tetris.UPDATE_INTERVAL = 50;
Tetris.UPDATE_RATIO_NORMAL = 5;
Tetris.UPDATE_RATIO_DOWN_PRESSED = 1;
Tetris.SCORE_PER_FLOOR = 100;

Tetris.MAX_HISTORY_COUNT = 5;

Tetris.prototype.currentScore = 0;
Tetris.prototype.gameContainer = null;
Tetris.prototype.previewArea = null;
Tetris.prototype.gameArea = null;
Tetris.prototype.movingCanvas = null;
Tetris.prototype.holdingCanvas = null;
Tetris.prototype.bgCanvas = null;
Tetris.prototype.gameOverPane = null;
Tetris.prototype.scorePane = null;
Tetris.prototype.previewCanvas = null;
Tetris.prototype.btnPlay = null;
Tetris.prototype.btnRanking = null;

Tetris.prototype.prepareBrick = null;
Tetris.prototype.movingBrick = null;
Tetris.prototype.holdingContext = null;
Tetris.prototype.movingContext = null;
Tetris.prototype.listenerAdded = false;
Tetris.prototype.disabledKeyborad = false;

Tetris.prototype.hSlice = 0;
Tetris.prototype.VSlice = 0;

Tetris.prototype.activeRatio = 5;
Tetris.prototype.updateCount = 0;

Tetris.prototype.brickSize = 0;
Tetris.prototype.containerWidth = 0;
Tetris.prototype.containerHeight = 0;

Tetris.prototype.activeTimerId = 0;

//Create game UI.
Tetris.prototype.initialize = function()
{
	this.previewArea = document.createElement("div");
	this.previewArea.style.background = "#CCCCCC";
	this.previewArea.style.width = String(Tetris.PREVIEW_AREA_WIDTH) + "px";
	this.previewArea.style.height = String(this.containerHeight) + "px";
	this.previewArea.style.cssFloat = "left";
	this.gameContainer.appendChild(this.previewArea);
	
	this.gameArea = document.createElement("div");
	this.gameArea.style.background = "#999999";
	this.gameArea.style.width = String(this.containerWidth - Tetris.PREVIEW_AREA_WIDTH) + "px";
	this.gameArea.style.height = String(this.containerHeight) + "px";
	this.gameArea.style.cssFloat = "left";
	this.gameContainer.appendChild(this.gameArea);
	
	this.hSlice = Math.floor((this.containerWidth - Tetris.PREVIEW_AREA_WIDTH) / this.brickSize);
	this.vSlice = Math.floor(this.containerHeight / this.brickSize);
	var canvasWidth =  this.hSlice * this.brickSize;
	var canvasHeight =  this.vSlice * this.brickSize;
	
	//create canvas for background.
	this.bgCanvas = document.createElement("canvas");
	this.bgCanvas.width = canvasWidth;
	this.bgCanvas.height = canvasHeight;
	this.bgCanvas.style.backgroundColor = "#000000";
	this.bgCanvas.style.zIndex = 1;
	this.bgCanvas.style.position = "absolute";
	this.bgCanvas.style.marginLeft = String((this.containerWidth - Tetris.PREVIEW_AREA_WIDTH - canvasWidth) / 2) + "px";
	this.bgCanvas.style.marginTop = String((this.containerHeight - canvasHeight) / 2) + "px";
	this.gameArea.appendChild(this.bgCanvas);
	
	//draw line on background.
	var bgContext = this.bgCanvas.getContext("2d");
	bgContext.strokeStyle = "#333333";
	for (var x = 0;x <= this.hSlice;x++)
	{
		bgContext.moveTo(x * this.brickSize,0);
		bgContext.lineTo(x * this.brickSize,canvasHeight);
	}
	for (var y = 0;y <= this.vSlice;y++)
	{
		bgContext.moveTo(0,y * this.brickSize);
		bgContext.lineTo(canvasWidth,y * this.brickSize);
	}
	bgContext.stroke();
	
	//create canvas for static bricks.
	this.holdingCanvas = document.createElement("canvas");
	this.holdingCanvas.width = canvasWidth;
	this.holdingCanvas.height = canvasHeight;
	this.holdingCanvas.style.zIndex = 2;
	this.holdingCanvas.style.position = "absolute";
	this.holdingCanvas.style.marginLeft = String((this.containerWidth - Tetris.PREVIEW_AREA_WIDTH - canvasWidth) / 2) + "px";
	this.holdingCanvas.style.marginTop = String((this.containerHeight - canvasHeight) / 2) + "px";
	this.gameArea.appendChild(this.holdingCanvas);
	
	//create canvas for move bricks.
	this.movingCanvas = document.createElement("canvas");
	this.movingCanvas.width = canvasWidth;
	this.movingCanvas.height = canvasHeight;
	this.movingCanvas.style.zIndex = 3;
	this.movingCanvas.style.position = "absolute";
	this.movingCanvas.style.marginLeft = String((this.containerWidth - Tetris.PREVIEW_AREA_WIDTH - canvasWidth) / 2) + "px";
	this.movingCanvas.style.marginTop = String((this.containerHeight - canvasHeight) / 2) + "px";
	this.gameArea.appendChild(this.movingCanvas);
	
	//create game over pane.
	this.gameOverPane = document.createElement("img");
	this.gameOverPane.src = "images/game_over.png";
	this.gameOverPane.style.display = "inline";
	this.gameOverPane.style.position = "absolute";
	this.gameOverPane.style.zIndex = 0;
	this.gameOverPane.onload = function(){
		this.style.marginLeft = String((this.parentNode.clientWidth - this.clientWidth) / 2) + "px";
		this.style.marginTop = String((this.parentNode.clientHeight - this.clientHeight) / 2) + "px";
		
	}
	this.gameArea.appendChild(this.gameOverPane);
	
	
	//create preview area element.
	this.scorePane = document.createElement("div");
	this.scorePane.innerHTML = "SCORE<br/>0";
	this.scorePane.style.width = "100%";
	this.scorePane.style.textAlign = "center";
	this.scorePane.style.marginTop = Tetris.PREVIEW_AREA_SPACE + "px";
	this.previewArea.appendChild(this.scorePane);
	
	// create preview canvas.
	this.previewCanvas = document.createElement("canvas");
	this.previewCanvas.width = Tetris.PREVIEW_CANVAS_WIDTH;
	this.previewCanvas.height = Tetris.PREVIEW_CANVAS_HEIGHT;
	this.previewCanvas.style.backgroundColor = "#000000";
	this.previewCanvas.style.marginLeft = (Tetris.PREVIEW_AREA_WIDTH - Tetris.PREVIEW_CANVAS_WIDTH) / 2 + "px";
	this.previewCanvas.style.marginTop =  Tetris.PREVIEW_AREA_SPACE + "px";
	this.previewArea.appendChild(this.previewCanvas);
	
	//create button area.
	var playDiv = document.createElement("div");
	playDiv.style.width = "100%";
	playDiv.style.textAlign = "center";
	this.previewArea.appendChild(playDiv);
	
	//create play button.	
	this.btnPlay = document.createElement("input");
	this.btnPlay.type = "button";
	this.btnPlay.value = Tetris.PLAY_BUTTON_TEXT;
	this.btnPlay.style.width = Tetris.BUTTON_WIDTH + "px";
	this.btnPlay.style.height = Tetris.BUTTON_SIZE + "px";
	this.btnPlay.style.marginTop = Tetris.PREVIEW_AREA_SPACE  + "px";
	playDiv.appendChild(this.btnPlay);
	this.btnPlay.addEventListener("click",function(evt){Tetris.gameApp.startGame()},false);
	
	//create ranking button.	
	this.btnRanking = document.createElement("input");
	this.btnRanking.type = "button";
	this.btnRanking.value = Tetris.RANKING_BUTTON_TEXT;
	this.btnRanking.style.width = Tetris.BUTTON_WIDTH + "px";
	this.btnRanking.style.height = Tetris.BUTTON_SIZE + "px";
	this.btnRanking.style.marginTop = Tetris.BUTTON_SPACE  + "px";
	playDiv.appendChild(this.btnRanking);
	this.btnRanking.addEventListener("click",function(evt){Tetris.gameApp.showRanking()})
}

Tetris.prototype.showRanking = function()
{
	if(!localStorage.scoreHistory)
		return;
	
	var history = localStorage.scoreHistory.split(",").join("\n");
	alert(history);
}

Tetris.prototype.resetGame = function()
{
	//clear all canvas.
	var holdingContext = this.holdingCanvas.getContext("2d");
	holdingContext.clearRect(0,0,this.holdingCanvas.width,this.holdingCanvas.height);
	var movingContext = this.movingCanvas.getContext("2d");
	movingContext.clearRect(0,0,this.movingCanvas.width,this.movingCanvas.height);
	var previewContext = this.previewCanvas.getContext("2d");
	previewContext.clearRect(0,0,this.previewCanvas.width,this.previewCanvas.height);
	
	//hide game over image.
	this.gameOverPane.style.zIndex = 0;
	
	//crate holding canvas.
	this.holdingContext = [];
	for(var y = 0;y < this.vSlice;y++)
	{
		var rowContext = [];
		for(var x = 0; x < this.hSlice; x++)
		{
			rowContext.push(null);
		}
		this.holdingContext.push(rowContext);
	}
	
	this.activeRatio = Tetris.UPDATE_RATIO_NORMAL;
	
	this.movingBrick = null;
	this.prepareBrick = null;
	
	clearInterval(this.activeTimerId);
	this.disabledKeyborad = false;
	
	this.currentScore = 0;
	this.scorePane.innerHTML = "SCORE<br/>0";
}

Tetris.prototype.startGame = function()
{
	this.resetGame();
	
	this.generateBrick();
	this.dropBrick();
	this.activeTimerId = setInterval(function(){Tetris.gameApp.brickSlumpping()},Tetris.UPDATE_INTERVAL);
	
	if(this.listenerAdded)
		return;
	this.listenerAdded = true;
		
	document.addEventListener("keydown",function(evt)
	{
		if(evt.keyCode < 37 || evt.keyCode > 40)
			return;
		Tetris.gameApp.onKeyDown(evt.keyCode);
	});
	
	document.addEventListener("keyup",function(evt)
	{
		if(evt.keyCode != 40)
			return;
		Tetris.gameApp.onDownKeyUp();
	});
}

Tetris.prototype.onDownKeyUp = function()
{
	if(this.disabledKeyborad)
		return;
		
	this.activeRatio = Tetris.UPDATE_RATIO_NORMAL;
}

Tetris.prototype.onKeyDown = function(keyCode)
{
	if(this.disabledKeyborad)
		return;
	
	this.movingBrick.clone();
		switch(keyCode)
		{
			case 37:
				this.movingBrick.x -= 1;
				break;
			
			case 39:
				this.movingBrick.x += 1;
				break;
			
			case 38:
				this.movingBrick.setRotation(this.movingBrick.angle + 90);
				break;
				
			case 40:
				this.activeRatio = Tetris.UPDATE_RATIO_DOWN_PRESSED;
				break;
		}
		
	this.correctBrick();
}


Tetris.prototype.brickSlumpping = function()
{
	this.updateCount ++;
	this.updateCount = this.updateCount > 1000 ? 0 : this.updateCount;
	
	if(this.updateCount % this.activeRatio != 0)
		return;

	this.movingBrick.clone();
	this.movingBrick.y += 1;
	this.correctBrick();
}

Tetris.prototype.correctBrick = function()
{
	if(this.movingBrick.y + this.movingBrick.getHeight() > this.vSlice)
	{
		//drop to bottom.
		this.movingBrick = this.movingBrick.clonedBrick;
		this.dropBrick();
		this.updateMovingCanvas();
		return;
	}
	
	this.movingBrick.x = Math.max(0,this.movingBrick.x);
	this.movingBrick.x = Math.min(this.hSlice - this.movingBrick.getWidth(),this.movingBrick.x);

	for(var i = 0;i < this.movingBrick.bricks.length;i++)
	{
		var brick = this.movingBrick.bricks[i];
		
		if(this.movingBrick.y + brick[0] < 0 || this.movingBrick.x + brick[1] < 0)
			continue;
		if(this.holdingContext[this.movingBrick.y + brick[0]][this.movingBrick.x + brick[1]] != null)
		{
			if(this.movingBrick.y <= 0)
			{
				this.gameOver();
				return;
			}
			
			// impact with other brick.
			var yOffset = this.movingBrick.y - this.movingBrick.clonedBrick.y;
			var roationChanged = this.movingBrick.angle - this.movingBrick.clonedBrick.angle;
			this.movingBrick = this.movingBrick.clonedBrick;
			
			if(yOffset > 0 && roationChanged == 0)
				this.dropBrick();
			
			this.updateMovingCanvas();
			return;
		}
	}
	
	this.updateMovingCanvas();
}

Tetris.prototype.gameOver = function()
{
	this.disabledKeyborad = true;
	
	clearInterval(this.activeTimerId);
	this.gameOverPane.style.display = "inline";
	this.gameOverPane.style.zIndex = 4;
	
	if(this.currentScore <= 0)
		return;
	
	if(localStorage.scoreHistory)
	{
		var history = localStorage.scoreHistory.split(",");
		history.push(this.currentScore);
		history.sort(function(a,b){ return Number(a) > Number (b) ? - 1 : 1});
		
		if(history.length > Tetris.MAX_HISTORY_COUNT)
			history = history.slice(0,Tetris.MAX_HISTORY_COUNT);
		
		localStorage.scoreHistory = history.join(",");
		
	}else{
		localStorage.scoreHistory = this.currentScore;
	}
}

Tetris.prototype.generateBrick = function()
{
	var type = Math.floor(Math.random() * 5);
	var directionType = Math.floor(Math.random() * 2);
	
	switch(type)
	{
		case 0:	
			brickContext = new LinerBrick;
			brickContext.setAngle = Math.floor(Math.random * 2) * 90;
			brickContext.fillColor = "#FF9900";
			break;
		
		case 1:		
			brickContext = directionType == 0 ? new LeftLightingBrick : new RightLightingBrick;
			brickContext.fillColor = "#00FF99";
			break;
		
		case 2:	
			brickContext = new FourBoxBrick;
			brickContext.fillColor = "#9900FF";
			break;
		
		case 3:		
			brickContext = directionType == 0 ? new LeftSevenBrick : new RightSevenBrick;
			brickContext.fillColor = "#99FF00";
			break;
		
		case 4:	
			brickContext = new ForkBrick;
			brickContext.fillColor = "#0099FF";
			break;
	}
	
	brickContext.setRotation(Math.floor(Math.random() * 5) * 90);
	this.prepareBrick = brickContext;
}

Tetris.prototype.updateHoldingCanvas = function()
{
	for(var i = 0;i < this.movingBrick.bricks.length;i++)
	{
		var brick = this.movingBrick.bricks[i];
		this.holdingContext[this.movingBrick.y + brick[0]][this.movingBrick.x + brick[1]] = new TetrisBrickUnit(this.movingBrick.fillColor,this.movingBrick.borderColor);
	}	
	
	for(var x = this.holdingContext.length - 1; x >= 0;x--)
	{
		var allFilled = true;
		for(var y = 0; y < this.holdingContext[x].length;y++)
			allFilled =  this.holdingContext[x][y] != null && allFilled;
		
		if (allFilled) 
		{
			var newLine = new Array(this.holdingContext[x].length);
			this.holdingContext.splice(x,1);
			this.holdingContext.unshift(newLine);
			x = x + 1;
			
			this.currentScore += Tetris.SCORE_PER_FLOOR;
		};	
	}
	
	this.scorePane.innerHTML = "SCORE<br/>" + String(this.currentScore);

	var context = this.holdingCanvas.getContext("2d");	
	context.clearRect(0,0,this.holdingCanvas.width,this.holdingCanvas.height);

	for(var h = 0; h < this.holdingContext.length;h++)
	{
		for(var v = 0;v < this.holdingContext[h].length;v++)
		{
			if(this.holdingContext[h][v] != null)
			{
				context.strokeStyle = this.holdingContext[h][v].borderColor;
				context.fillStyle = this.holdingContext[h][v].fillColor;
				
				context.strokeRect(this.brickSize * v,this.brickSize * h,this.brickSize,this.brickSize);
				context.fillRect(this.brickSize * v,this.brickSize * h,this.brickSize,this.brickSize);
			}
		}
	}
	
}

Tetris.prototype.previewBrick = function()
{
	var startX = (Tetris.PREVIEW_CANVAS_WIDTH - (this.prepareBrick.getWidth() * Tetris.PREVIEW_BRICK_SIZE)) / 2;
	var startY = (Tetris.PREVIEW_CANVAS_HEIGHT - (this.prepareBrick.getHeight() * Tetris.PREVIEW_BRICK_SIZE)) / 2;
	
	var context = this.previewCanvas.getContext("2d");
	context.clearRect(0,0,this.previewCanvas.width,this.previewCanvas.height);
	
	context.strokeStyle = this.prepareBrick.borderColor;
	context.fillStyle = this.prepareBrick.fillColor;
	for(var i = 0;i < this.prepareBrick.bricks.length;i++)
	{
		context.strokeRect(startX + this.prepareBrick.bricks[i][1] * Tetris.PREVIEW_BRICK_SIZE,
						   startY + this.prepareBrick.bricks[i][0] * Tetris.PREVIEW_BRICK_SIZE,
						   Tetris.PREVIEW_BRICK_SIZE,Tetris.PREVIEW_BRICK_SIZE);
		context.fillRect(startX + this.prepareBrick.bricks[i][1] * Tetris.PREVIEW_BRICK_SIZE,
						 startY + this.prepareBrick.bricks[i][0] * Tetris.PREVIEW_BRICK_SIZE,
						 Tetris.PREVIEW_BRICK_SIZE,Tetris.PREVIEW_BRICK_SIZE);
	}
	
	
	
}

Tetris.prototype.dropBrick = function()
{
	if(this.movingBrick != undefined)
	{
		this.updateHoldingCanvas();	
	}
	
	this.movingBrick = this.prepareBrick;
	this.movingBrick.x = Math.floor((this.hSlice - this.movingBrick.getWidth()) / 2);
	this.movingBrick.y = 0 - this.movingBrick.getHeight();
	this.generateBrick();
	this.previewBrick();
}

Tetris.prototype.updateMovingCanvas = function()
{
	var context = this.movingCanvas.getContext("2d");
	context.clearRect(0,0,this.movingCanvas.width,this.movingCanvas.height);	
	
	context.strokeStyle = this.movingBrick.borderColor;
	context.fillStyle = this.movingBrick.fillColor;
	for(var i = 0;i < this.movingBrick.bricks.length;i++)
	{
		context.strokeRect(this.movingBrick.x * this.brickSize +  this.movingBrick.bricks[i][1] * this.brickSize,
						   this.movingBrick.y * this.brickSize + this.movingBrick.bricks[i][0] * this.brickSize,
						   this.brickSize,this.brickSize);
		context.fillRect(this.movingBrick.x * this.brickSize + this.movingBrick.bricks[i][1] * this.brickSize,
						 this.movingBrick.y * this.brickSize + this.movingBrick.bricks[i][0] * this.brickSize,
						 this.brickSize,this.brickSize);
	}
}


//class for bricks.
var TetrisBrick = function(){}

TetrisBrick.prototype.x = 0;
TetrisBrick.prototype.y = 0;
TetrisBrick.prototype.angle = 0;
TetrisBrick.prototype.fillColor = "#FF9900";
TetrisBrick.prototype.borderColor = "#FFFFFF";

TetrisBrick.prototype.bricks = [];

TetrisBrick.prototype.clonedBrick = null;
TetrisBrick.prototype.clone = function()
{
	this.clonedBrick = new TetrisBrick;
	this.clonedBrick.x = this.x;
	this.clonedBrick.y = this.y;
	this.clonedBrick.angle = this.angle;
	this.clonedBrick.fillColor = this.fillColor;
	this.clonedBrick.borderColor = this.borderColor;
	this.clonedBrick.bricks = this.bricks;
}

TetrisBrick.prototype.getWidth = function()
{
	var maxX,minX;
	
	for(var i= 0;i < this.bricks.length;i++)
	{
		if(isNaN(maxX))
			maxX = this.bricks[i][1];
		else
			maxX = Math.max(maxX,this.bricks[i][1]);
			
		if(isNaN(minX))
			minX = this.bricks[i][1];
		else
			minX = Math.min(minX,this.bricks[i][1]);	
	}
	
	return maxX - minX + 1;
}

TetrisBrick.prototype.getHeight = function()
{
	var maxY,minY;
	
	for(var i= 0;i < this.bricks.length;i++)
	{
		if(isNaN(maxY))
			maxY = this.bricks[i][0];
		else
			maxY = Math.max(maxY,this.bricks[i][0]);
			
		if(isNaN(minY))
			minY = this.bricks[i][0];
		else
			minY = Math.min(minY,this.bricks[i][0]);	
	}
	
	return maxY - minY + 1;
}


// line brick, type 0.
var LinerBrick = function(){}

LinerBrick.prototype = new TetrisBrick;
LinerBrick.prototype.setRotation = function(angle)
{
	angle = (angle / 90) %2 == 0 ? 0 : 90;
	if(angle == this.angle && this.bricks.length > 0)
		return;
	
	this.angle = angle;	
	
	if(this.angle == 0)
	{
		this.bricks = [[0,0],[1,0],[2,0],[3,0]];
	
		if(this.bricks.length <= 0)
			return;
			
		this.y -= 1;
		this.x += 1
	}else{
		this.bricks = [[0,0],[0,1],[0,2],[0,3]];
	
		if(this.bricks.length <= 0)
			return;
	
		this.y += 1;
		this.x -= 1;
	}
}

LinerBrick.prototype.clone = function()
{
	this.clonedBrick = new LinerBrick;
	this.clonedBrick.x = this.x;
	this.clonedBrick.y = this.y;
	this.clonedBrick.angle = this.angle;
	this.clonedBrick.fillColor = this.fillColor;
	this.clonedBrick.borderColor = this.borderColor;
	this.clonedBrick.bricks = this.bricks;
}


// left lighting brick. type 1.
var LeftLightingBrick = function(){}

LeftLightingBrick.prototype = new TetrisBrick;
LeftLightingBrick.prototype.setRotation = function(angle)
{
	angle = (angle / 90) %2 == 0 ? 0 : 90;
	if(angle == this.angle && this.bricks.length > 0)
		return;
	
	this.angle = angle;	
	
	if(this.angle == 0)
	{
		this.bricks = [[0,0],[1,0],[1,1],[2,1]];
	
		if(this.bricks.length <= 0)
			return;
			
		this.y -= 1;
		this.x += 1
	}else{
		this.bricks = [[0,1],[0,2],[1,1],[1,0]];
	
		if(this.bricks.length <= 0)
			return;
	
		this.y += 1;
		this.x -= 1;
	}
}

LeftLightingBrick.prototype.clone = function()
{
	this.clonedBrick = new LeftLightingBrick;
	this.clonedBrick.x = this.x;
	this.clonedBrick.y = this.y;
	this.clonedBrick.angle = this.angle;
	this.clonedBrick.fillColor = this.fillColor;
	this.clonedBrick.borderColor = this.borderColor;
	this.clonedBrick.bricks = this.bricks;
}


// right lighting brick. type 2.
var RightLightingBrick = function(){}

RightLightingBrick.prototype = new TetrisBrick;
RightLightingBrick.prototype.setRotation = function(angle)
{
	angle = (angle / 90) %2 == 0 ? 0 : 90;
	
	if(angle == this.angle && this.bricks.length > 0)
		return;
	this.angle = angle;	
	
	if(this.angle == 0)
		this.bricks = [[0,1],[1,1],[1,0],[2,0]];
	else
		this.bricks = [[0,0],[0,1],[1,1],[1,2]];
}

RightLightingBrick.prototype.clone = function()
{
	this.clonedBrick = new RightLightingBrick;
	this.clonedBrick.x = this.x;
	this.clonedBrick.y = this.y;
	this.clonedBrick.angle = this.angle;
	this.clonedBrick.fillColor = this.fillColor;
	this.clonedBrick.borderColor = this.borderColor;
	this.clonedBrick.bricks = this.bricks;
}

// four box brick. type 3.
var FourBoxBrick = function(){}

FourBoxBrick.prototype = new TetrisBrick;
FourBoxBrick.prototype.setRotation = function(angle)
{
	angle = (angle / 90) %2 == 0 ? 0 : 90;
	
	if(angle == this.angle && this.bricks.length > 0)
		return;
	this.angle = angle;	
	this.bricks = [[0,0],[0,1],[1,1],[1,0]];
}

FourBoxBrick.prototype.clone = function()
{
	this.clonedBrick = new FourBoxBrick;
	this.clonedBrick.x = this.x;
	this.clonedBrick.y = this.y;
	this.clonedBrick.angle = this.angle;
	this.clonedBrick.fillColor = this.fillColor;
	this.clonedBrick.borderColor = this.borderColor;
	this.clonedBrick.bricks = this.bricks;
}

// left seven brick. type 4.
var LeftSevenBrick = function(){}

LeftSevenBrick.prototype = new TetrisBrick;
LeftSevenBrick.prototype.setRotation = function(angle)
{
	var mulriple = (angle / 90) % 4;
	switch(mulriple)
	{
		case 0:
			angle = 0;
			break;
		case 1:
			angle = 90;
			break;
		case 2:
			angle = 180;
			break;
		case 3:
			angle = 270;
			break;
	}

	if(angle == this.angle && this.bricks.length > 0)
		return;
	
	this.angle = angle;	
	
	if(this.angle == 0)
	{
		this.bricks = [[0,0],[0,1],[1,1],[2,1]];
	
		if(this.bricks.length <= 0)
			return;
		
		this.x -= 1;
	}else if(this.angle == 90){
		this.bricks = [[1,0],[1,1],[1,2],[0,2]];
	
		if(this.bricks.length <= 0)
			return;
	
		this.y -= 1;
		this.x -= 1;
	}else if(this.angle == 180){
		this.bricks = [[0,0],[1,0],[2,0],[2,1]];
	
		if(this.bricks.length <= 0)
			return;
	
		this.y -= 1;
		this.x += 2;
	}else if(this.angle == 270){
		this.bricks = [[0,0],[0,1],[0,2],[1,0]];
	
		if(this.bricks.length <= 0)
			return;
	
		this.y += 1;
		this.x -= 1;
	}
}

LeftSevenBrick.prototype.clone = function()
{
	this.clonedBrick = new LeftSevenBrick;
	this.clonedBrick.x = this.x;
	this.clonedBrick.y = this.y;
	this.clonedBrick.angle = this.angle;
	this.clonedBrick.fillColor = this.fillColor;
	this.clonedBrick.borderColor = this.borderColor;
	this.clonedBrick.bricks = this.bricks;
}

// right seven brick. type 5.
var RightSevenBrick = function(){}

RightSevenBrick.prototype = new TetrisBrick;
RightSevenBrick.prototype.setRotation = function(angle)
{
	var mulriple = (angle / 90) % 4;
	switch(mulriple)
	{
		case 0:
			angle = 0;
			break;
		case 1:
			angle = 90;
			break;
		case 2:
			angle = 180;
			break;
		case 3:
			angle = 270;
			break;
	}

	if(angle == this.angle && this.bricks.length > 0)
		return;
	
	this.angle = angle;	
	
	if(this.angle == 0)
	{
		this.bricks = [[0,1],[0,0],[1,0],[2,0]];
	
		if(this.bricks.length <= 0)
			return;
		
		this.y -= 1;
	}else if(this.angle == 90){
		this.bricks = [[0,0],[0,1],[0,2],[1,2]];
	
		if(this.bricks.length <= 0)
			return;
			
		this.x -= 2;
	}else if(this.angle == 180){
		this.bricks = [[0,1],[1,1],[2,1],[2,0]];
	
		if(this.bricks.length <= 0)
			return;
	
		this.y -= 2;
		this.x += 1;
	}else if(this.angle == 270){
		this.bricks = [[0,0],[1,0],[1,1],[1,2]];
	
		if(this.bricks.length <= 0)
			return;
	
		this.y += 1;
		this.x += 1;
	}
}


RightSevenBrick.prototype.clone = function()
{
	this.clonedBrick = new RightSevenBrick;
	this.clonedBrick.x = this.x;
	this.clonedBrick.y = this.y;
	this.clonedBrick.angle = this.angle;
	this.clonedBrick.fillColor = this.fillColor;
	this.clonedBrick.borderColor = this.borderColor;
	this.clonedBrick.bricks = this.bricks;
}

// frok brick. type 6.
var ForkBrick = function(){}

ForkBrick.prototype = new TetrisBrick;
ForkBrick.prototype.setRotation = function(angle)
{
	var mulriple = (angle / 90) % 4;
	switch(mulriple)
	{
		case 0:
			angle = 0;
			break;
		case 1:
			angle = 90;
			break;
		case 2:
			angle = 180;
			break;
		case 3:
			angle = 270;
			break;
	}

	if(angle == this.angle && this.bricks.length > 0)
		return;
	
	this.angle = angle;	
	
	if(this.angle == 0)
	{
		this.bricks = [[0,0],[1,0],[1,1],[2,0]];
	
		if(this.bricks.length <= 0)
			return;
		
		this.x += 1;
	}else if(this.angle == 90){
		this.bricks = [[0,0],[0,1],[0,2],[1,1]];
	
		if(this.bricks.length <= 0)
			return;
			
		this.x -= 1;
		this.y += 1
	}else if(this.angle == 180){
		this.bricks = [[0,1],[1,1],[2,1],[1,0]];
	
		if(this.bricks.length <= 0)
			return;
	
		this.y -= 1;
	}else if(this.angle == 270){
		this.bricks = [[0,1],[1,0],[1,1],[1,2]];
	
		if(this.bricks.length <= 0)
			return;
	}
}


ForkBrick.prototype.clone = function()
{
	this.clonedBrick = new ForkBrick;
	this.clonedBrick.x = this.x;
	this.clonedBrick.y = this.y;
	this.clonedBrick.angle = this.angle;
	this.clonedBrick.fillColor = this.fillColor;
	this.clonedBrick.borderColor = this.borderColor;
	this.clonedBrick.bricks = this.bricks;
}



//class for present brick unit.
var TetrisBrickUnit = function(fillColr,borderColor)
{	
	this.fillColor = fillColr;
	this.borderColor = borderColor;
}
